/* $RCSFile: ViewControlButtons.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/ADNCRM.Core/UI/WebControls/ViewControlButtons.cs $
 * $log$
 * Revision 6 2010/08/06 19:56:30  christian.surieux
 *   Added Log to All cs files
 */

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
//using System.Drawing;
using System.IO;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Text;

using AddonNice.UI;
using AddonNice.Helpers;
using AddonNice.Security;
using AddonNice.Configuration;
using AddonNice.UI.WebControls;
using AddonNice.BLL.User;
using AddonNice.BLL.UserConfig;
using AddonNice.BLL.Utils;

//
// This Namespace holds the information to manage the viewable controls;
// namely, min., max. and close buttons
// 
namespace AddonNice.UI.WebControls
{
	/// <summary>
	/// This class contains the data 
	/// </summary>
	internal class ViewControlData
	{
		string _name				 = string.Empty; // button name
		string _alt_text			 = string.Empty; // alternate text
		string _localize			 = string.Empty; // localized tag name
		LinkButton _button			 = null; // the control

		/// <summary>
		/// ctor to create the image button
		/// </summary>
		public ViewControlData()
		{}

		/// <summary>
		///  ctor to create the image button
		/// </summary>
		public ViewControlData(string name,string localize) : this(name,localize,localize)
		{
		}

		/// <summary>
		/// Main ctor to create the image button
		/// </summary>
		public ViewControlData(string name,string altText,string localize)
		{
			this._name		= name;
			this._alt_text	= altText;
			this._localize	= localize;
			this._button	= new LinkButton();

			// setup the button attributes
			setup();

		} // end of ctor


		/// <summary>
		/// Attribute for the Image Button
		/// </summary>
		public LinkButton viewControlBtn 
		{
			get { return this._button; }
			set { this._button = value; }
		} // end of viewControlBtn

		/// <summary>
		/// Attribute for the Image Button Name
		/// </summary>
		public string name 
		{
			get { return this._name; }
			set { this._name = value; }

		} // end of Name

		/// <summary>
		/// Attribute for the Image Button Alternate Text
		/// </summary>
		public string alternateText 
		{
			get {return this._alt_text; }
			set { this._alt_text = value; }

		} // end of AlternateText

		/// <summary>
		/// Attribute for the Image Button Localized Text
		/// </summary>
		public string localized 
		{
			get {return this._localize; }
			set { this._localize = value; }

		} // end of AlternateText


		/// <summary>
		///  enable the view state of the button
		/// </summary>
		public void enableViewState(bool state)
		{
			if ( this._button != null )
			{
				this._button.EnableViewState=state;
			}
		} // end of enableViewState

		/// <summary>
		///  enable the visibility  of the button
		/// </summary>
		public void visible(bool state)
		{
			if ( this._button != null )
			{
				this._button.Visible=state;
			}
		} // end of visible

		/// <summary>
		/// Get the html format of the image
		/// </summary>
		internal static string ImageFormat(string name)
		{
            return string.Format("<img border=0 SkinID='{0}' />", name);
		} 

		
		/// <summary>
		/// setup the button
		/// </summary>
		private void setup()
		{
			if ( this._button != null )
			{
				// url
				string url				                = ImageFormat(this.name);
				// if no url, then it's not wanted
				if ( url == null || url.Length==0 )
					this._button                        = null ;
				else 
				{
					// button name used for determining type
					this._button.Attributes["bname"]    = this.name;
					// tool tip (  use localize string )
                    this._button.ToolTip                = LocalizeHelper.Localize(localized, alternateText);
					// don't validate
					this._button.CausesValidation       = false;
					// set the image
                    this._button.Text = url;//CS 17/1/06 ImageFormat(this.name);
				}
			}
		} // end of setup

	} // end of ViewControlData

	/// ----------------------------------------------------------------------
	/// <summary>
	/// This manages the collection of view control button data
	/// </summary>
	public class ViewControlManager
	{
		// collection of buttons
		ArrayList _buttons	= new ArrayList();
		int _tabID			= -1;
		int _moduleID		= -1;
		string _rawUrl		= string.Empty;

		/// <summary>
		/// Default Ctor
		/// </summary>
		public ViewControlManager(int tabID,int moduleID,string rawUrl )
		{
			// what tab 
			_tabID	        = tabID;
			// what module
			_moduleID       = moduleID;
			// what url
			_rawUrl         = rawUrl;
		} // end of ctor

		/// <summary>
		/// Returns a the  button, if present
		/// </summary>
		public LinkButton  this[ string index ]
		{
			get 
			{
				// find control
				ViewControlData vcd = findControl(index);
				LinkButton btn = null;
				// if present assign it
				if ( vcd != null )
					btn= (LinkButton)vcd.viewControlBtn;
				return btn;
			}
		
		} // end of LinkButton


		/// <summary>
		/// Return the module ID
		/// </summary>
		public int ModuleID
		{
			get { return this._moduleID; }
		}
		/// <summary>
		/// Return the Tab id
		/// </summary>
		public int TabID
		{
			get { return this._tabID; }
		}
		/// <summary>
		///  Create the Image Button
		/// </summary>
		public LinkButton create ( string name, string local)
		{
			return this.create(name, local, local);
		} // end of create

		/// <summary>
		/// Create the Image Button
		/// </summary>
		public LinkButton create(string name, string altText, string localize)
		{
			ViewControlData vcd =  this.findControl(name);
			// if present, then overwite
			if ( vcd == null )
			{
				// create a new view control
				vcd = new ViewControlData(name, altText, localize);
				// set event handlers
				vcd.viewControlBtn.Click += new EventHandler(this._buttonClick);
				// set attributes
				vcd.viewControlBtn.Attributes[StStr.strModuleID] = this._moduleID.ToString();
				vcd.viewControlBtn.Attributes["tID"] = this._tabID.ToString();
				// add to list
				_buttons.Add(vcd);
			}

			return vcd.viewControlBtn;

		} // end of create


		/// <summary>
		/// Find the control data
		/// </summary>
		private ViewControlData findControl(string index)
		{
			foreach ( ViewControlData vcd in _buttons )
				if ( vcd.name == index )
					return vcd;
			return null;
		} // end of findControl

		/// <summary>
		/// This method gets called when user min/max the button
		/// </summary>
		private void _buttonClick(Object sender, EventArgs evt)
		{
			// 
			LinkButton ibut         = (LinkButton) sender	;
			WindowStateEnum state   = WindowStateEnum.Minimized;
			string name			    = ibut.Attributes["bname"];
			int mID				    = Int32.Parse(ibut.Attributes[StStr.strModuleID]);
			int tadID			    = Int32.Parse(ibut.Attributes["tID"]);
	
			// what state are we in
			if ( name != null )
			{
				if ( name == WindowStateStrings.ButtonMaxName )
				{
					state           =  WindowStateEnum.Maximized;				
				} 
				else if ( name == WindowStateStrings.ButtonCloseName )// close
				{
					state           =  WindowStateEnum.Closed;				
				}
				// set the state of visibility
				UserDesktop.UpdateUserDesktop(mID,state,tadID);
			}
            if ( ibut.Page is ADNPage )
			    ((ADNPage)ibut.Page).Redirect(HttpContext.Current.Request.RawUrl,true);
			else    HttpContext.Current.Response.Redirect(HttpContext.Current.Request.RawUrl);
		} // end of _buttonClick

	} // end of ViewControlManager
}